package com.zut.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zut.common.BaseContext;
import com.zut.common.R;
import com.zut.entity.ShoppingCart;
import com.zut.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
@Autowired
private ShoppingCartService shoppingCartService;


    @GetMapping("/list")
    public R<List<ShoppingCart>> list()
    {   Long userId = BaseContext.getCurrentId();
        log.info("userId:"+userId);
        LambdaQueryWrapper <ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(!Objects.isNull(userId),ShoppingCart::getUserId,userId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> cartList = shoppingCartService.list(queryWrapper);
        log.info("cartList:"+cartList.toString());
         return R.success(cartList);
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody  ShoppingCart shoppingCart){
         Long userId = BaseContext.getCurrentId();
         shoppingCart.setUserId(userId);
         //查询当前菜品或者套餐是否在表里
         LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(ShoppingCart::getUserId,userId);
         queryWrapper.eq(!Objects.isNull(shoppingCart.getDishId()),ShoppingCart::getDishId,shoppingCart.getDishId());
         queryWrapper.eq(!Objects.isNull(shoppingCart.getSetmealId()),ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
         ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
         if(cartServiceOne!=null){
             cartServiceOne.setNumber(cartServiceOne.getNumber()+1);

             shoppingCartService.updateById(cartServiceOne);
         }else{
             shoppingCart.setNumber(1);
             shoppingCart.setCreateTime(LocalDateTime.now());
             shoppingCartService.save(shoppingCart);

             cartServiceOne = shoppingCart;
         }
         return R.success(cartServiceOne);
    }
   @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        queryWrapper.eq(!Objects.isNull(shoppingCart.getDishId()),ShoppingCart::getDishId,shoppingCart.getDishId());
        queryWrapper.eq(!Objects.isNull(shoppingCart.getSetmealId()),ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if(cartServiceOne.getNumber()==1){
            shoppingCartService.remove(queryWrapper);
        }
        cartServiceOne.setNumber(cartServiceOne.getNumber()-1);

        shoppingCartService.updateById(cartServiceOne);
        return R.success(cartServiceOne);
    }
}
